package com.aidemoconfidence.conf.Controller;

import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class conf {
    @Resource(name="resultRepository")
    ResultRepository resultRepository;

    @GetMapping(value = "/AIDemoConfidence/GetAllUser")
    public List<String> GetAllUser(){
        return resultRepository.GetAllUser();
    }

    @GetMapping(value = "/AIDemoConfidence/GetRootInfo")
    public List<Map<String,String>> GetRootInfo(){
        return resultRepository.GetRootInfo();
    }

    @GetMapping(value = "/AIDemoConfidence/GetSyscode")
    public List<String> GetSyscode(){
        return resultRepository.GetSyscode();
    }

    @PostMapping(value = "/AIDemoConfidence/AddRoot")
    public int AddRoot(@RequestBody Map<String,String> params){
        String nickname = params.get("nickname");
        int id = GetMaxID()+1;
        return resultRepository.AddRoot(params.get("syscode"),nickname,GetOpenid(nickname),id);
    }

    @PostMapping(value = "/AIDemoConfidence/DeleteRoot")
    public int DeleteRoot(@RequestBody Map<String,String> params){
        return resultRepository.DeleteRoot(params.get("syscode"),params.get("nickname"));
    }

    private int GetMaxID(){
        return resultRepository.GetMaxID();
    }

    private String GetOpenid (String nickname){
        return resultRepository.GetOpenid(nickname);
    }

    @GetMapping(value = "/GetHttpDemo")
    public void GetHttpDemo(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8099/AIDemoConfidence/GetAllUser";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        System.out.println(response.getBody());
    }
}
